<template>
  <div class="login-container">
    <!-- <div class="systemName"><img src="@/assets/img/loginLogo.png" alt="" /></div>
    <div>
      <img src="@/assets/img/loginColor.png" alt="" />
    </div> -->
    <div class="login-form">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" autocomplete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">用户登录</h3>
        </div>

        <el-form-item prop="username">
          <!-- <span class="svg-container">
            <svg-icon icon-class="user" />
          </span> -->
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入用户名"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
            maxlength="50"
          />
        </el-form-item>

        <el-form-item prop="password">
          <!-- <span class="svg-container">
            <svg-icon icon-class="password" />
          </span> -->
          <el-input
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            maxlength="50"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width: 100%" @click.native.prevent="handleLogin"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { encrypt } from '@/api/encrypt'
import { showErrorMsg } from '@/api/msgutils'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    // const validatePassword = (rule, value, callback) => {
    //   callback()
    // }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      passwordType: 'password',

      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },

  created() {},
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {},
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          const userInfoForm = {}
          userInfoForm.userName = this.loginForm.username
          userInfoForm.passWord = encrypt(this.loginForm.password)
          this.$store
            .dispatch('user/login', userInfoForm)
            .then(() => {
              // this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.$router.push({
                path: '/'
              })
              this.loading = false
            })
            .catch(res => {
              showErrorMsg(res)
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // 联调前
    // this.$router.push({ path: '/dashboard' })
    // 联调前
  }
}
</script>

<style lang="scss" scoped>
$bg: #283443;
$light_gray: #000;
$cursor: #000;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        // box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  button {
    height: 54px;
    border-radius: 27px;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #f4f4f9;
    border-radius: 5px;
    color: #454545;
    margin-bottom: 40px;
    height: 60px;
    border-radius: 30px;
    padding: 12px 20px;
  }
  ::v-deep {
    .el-input__inner {
      background: #f4f4f9;
      border: none;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #cfd0da;
$light_gray: #eee;
.systemName {
  position: fixed;
  top: 110px;
  left: 240px;
  font-size: 32px;
  font-family: TRENDS;
  color: #ffffff;
  line-height: 38px;
  letter-spacing: 1px;
  height: 38px;
  font-family: Cursive;
  img {
    vertical-align: text-top;
  }
  span {
    font-size: 14px;
    font-weight: 500;
    color: #264598;
    line-height: 24px;
    width: 60px;
    height: 24px;
    background: linear-gradient(180deg, #deeeff 0%, #a0bdeb 100%);
    border-radius: 5px;
    display: inline-block;
    text-align: center;
    font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
  }
}
.login-container {
  min-height: 90vh;
  height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-image: url(~@/assets/img/loginBg.png);
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  display: flex;
  align-items: center;
  //justify-content: space-around;
  .login-form {
    width: 520px;
    height: 420px;
    // margin: 0 auto;
    overflow: hidden;
    background: #ffffff;
    box-shadow: 0px 0px 30px 0px rgba(0, 0, 0, 0.28);
    padding: 40px 60px 0 60px;
    border-radius: 12px;
    position: absolute;
    right: 20%;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-weight: bold;
      font-size: 22px;
      color: #333333;
      line-height: 33px;
      margin-bottom: 20px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 4px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .checkbox {
    margin-top: 12px;
    text-align: right;
    ::v-deep {
      .el-checkbox__input.is-checked + .el-checkbox__label {
        color: #000;
      }
      .el-checkbox__label {
        font-size: 12px;
        padding-left: 5px;
      }
    }
  }
}
.validate-code {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  cursor: pointer;
}
</style>
